// attrition //

// survey data //

	use "input/20190909 - Base ABC Cerrado.dta" , clear

	gen yob = year(V01010400)
	keep if time==1
	keep V00000005 tr_Z yob

	save "output/resp.dta", replace
	
// administrative data 

	use "input/prod_turma_inscricoes_agosto_17.dta", clear 
	keep if  Curso=="RECUPERAçãO DE PASTAGENS DEGRADADAS"

	replace V00000005="33115273304" if V00000005=="33115213304"
	gen terminou = ordem_cap_ateg!=.

	gen T0 = (universo_pesquisados==0)
	gen T1 = (terminou==1 & grupos_exp!=3)
	gen T2 = grupos_exp==3
	drop if T0==0 & T1==0 & T2==0
	
	replace Sexo = trim(Sexo)
	gen male = Sexo=="M"
	
	replace Datadenascimento=trim(Datadenascimento)
	gen anonascim = substr(Datadenascimento,-4,.)
	destring anonascim, replace
	replace anonascim=1970 if anonascim<1900 | anonascim==.
	
	replace Graudeescolaridade=trim(Graudeescolaridade)
	gen hs = (Graudeescolaridade=="Ensino médio completo" | Graudeescolaridade=="Ensino superior incompleto" | Graudeescolaridade=="Técnico")
	gen cg = (Graudeescolaridade=="Ensino superior completo" | Graudeescolaridade=="Pós-graduado")
	
	gen area = Tamanhoemhectaresha
	
	gen expl = Áreaprodutiva
	
	replace CadastronoCAR=trim(CadastronoCAR)
	gen car = ( CadastronoCAR=="Sim")
	
	gen workers = Funcionárioscontratados
	
	replace Nosúltimosanosofaturamento=trim(Nosúltimosanosofaturamento) 
	gen big = Nosúltimosanosofaturamento=="R$ 360 mil ou superior"

	gen fam_labor = Membrosdafamília
	
	keep V00000005 T0 T1 T2 male anonascim hs cg area expl car workers big fam_labor
	
	merge 1:1 V00000005 using "output/resp.dta", nogen 
	
	gen resp = (tr_Z!=.)
	
	save "output/admin.dta", replace
	
// attrition test

	use "output/admin.dta", clear	
	
	// training  	
	
	foreach depvar in male hs cg area expl car fam_labor workers big {
		attregtest `depvar' if T2==0, treatvar(T1) respvars(resp) vce(robust)
		scalar IVR1`depvar' = `e(p_IVR)'
		loc IVR1`depvar': display %4.3f IVR1`depvar'
		scalar IVP1`depvar' = `e(p_IVP)'
		loc IVP1`depvar': display %4.3f IVP1`depvar'
	}
	
	foreach depvar in male hs cg area expl car fam_labor workers big {
		attregtest `depvar' if T0==0, treatvar(T2) respvars(resp) vce(robust)
		scalar IVR2`depvar' = `e(p_IVR)'
		loc IVR2`depvar': display %4.3f IVR2`depvar'
		scalar IVP2`depvar' = `e(p_IVP)'
		loc IVP2`depvar': display %4.3f IVP2`depvar'
	}
	
file open Table using "tables/tableS3.tex", write replace
		
file write Table "\begin{table}[h!]" _n 
file write Table "\centering" _n
file write Table "\small" _n
file write Table "\caption{Tests of Differential Attrition}" _n
file write Table "\label{tab:effects1}" _n
file write Table "\begin{tabularx} {\linewidth} {@{} l Z Z Z Z}" _n
file write Table "\toprule" _n
file write Table "\toprule" _n
file write Table "& \multicolumn{2}{c}{T1 vs T0} & \multicolumn{2}{c}{T2 vs T1} \\" _n
file write Table "\cmidrule(l{.75em}){2-3} \cmidrule(l{.75em}){4-5}" _n
file write Table " & IV-R & IV-P & IV-R & IV-P \\" _n		
file write Table "\cmidrule(l{.75em}){2-3} \cmidrule(l{.75em}){4-5}" _n
file write Table " & (1) & (2) & (3) & (4) \\" _n
file write Table "\midrule" _n
file write Table "Male (0/1) & `IVR1male' & `IVP1male' & `IVR2male' & `IVP2male' \\" _n
file write Table "High School Graduate (0/1) & `IVR1hs' & `IVP1hs' & `IVR2hs' & `IVP2hs' \\" _n
file write Table "College Graduate (0/1) & `IVR1cg' & `IVP1cg' & `IVR2cg' & `IVP2cg' \\" _n
file write Table "Area & `IVR1area' & `IVP1area' & `IVR2area' & `IVP2area' \\" _n
file write Table "Area Explored & `IVR1expl' & `IVP1expl' & `IVR2expl' & `IVP2expl' \\" _n
file write Table "Registry (0/1) & `IVR1car' & `IVP1car' & `IVR2car' & `IVP2car' \\" _n
file write Table "Family labor & `IVR1fam_labor' & `IVP1fam_labor' & `IVR2fam_labor' & `IVP2fam_labor' \\" _n
file write Table "Hired labor & `IVR1workers' & `IVP1workers' & `IVR2workers' & `IVP2workers' \\" _n
file write Table "Revenues $>$ R\$ 360,000 & `IVR1big' & `IVP1big' & `IVR2big' & `IVP2big' \\" _n
file write Table "N & \multicolumn{2}{c}{1058} & \multicolumn{2}{c}{706} \\" _n
file write Table "\bottomrule" _n
file write Table "\bottomrule" _n
file write Table "\end{tabularx}" _n
file write Table "\begin{tablenotes}" _n
file write Table "\small" _n
file write Table "\begin{spacing}{1.0}" _n
file write Table "\item Notes: {Each row reports the results for a different outcome. Column 1 reports the coefficient of a bivariate regression of each outcome on the dummy indicating that the farm was offered technical assistance. Column 2 adds the outcome itself measured in the baseline as a control. Column 3 adds the pre-determined characteristics as additional controls. The sample is restricted to farmers who completed the training. Robust standard errors are reported in parentheses. *** p<0.01; ** p<0.05; * p<0.10}" _n
file write Table "\end{spacing}" _n
file write Table "\end{tablenotes}" _n
file write Table "\end{table}%" _n
file close Table
	

	

